package com.jtjrenren.android.taxi.passenger.core.order.complaint;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.jtjrenren.android.taxi.passenger.core.adapter.CompaintReplayAdapter;
import com.jtjrenren.android.taxi.passenger.R;
import com.jtjrenren.android.taxi.passenger.bean.api.ApiResultCommonStr;
import com.jtjrenren.android.taxi.passenger.bean.api.Complaint;
import com.jtjrenren.android.taxi.passenger.bean.api.ComplaintList;
import com.jtjrenren.android.taxi.passenger.manger.AppManger;
import com.jtjrenren.android.taxi.passenger.manger.api.ApiWorks;
import com.jtjrenren.android.taxi.passenger.core.adapter.CompaintSugAdapter;
import com.jtjrenren.android.taxi.passenger.base.BaseActivity;
import com.jtjrenren.android.taxi.passenger.utils.InitView;
import com.jtjrenren.android.taxi.passenger.utils.ModuleUtils;

import com.library.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/1/31.
 */
public class DriverComplaintActivity extends BaseActivity {
    public static final String EXTRAS_ORDERNO = "orderno";
    public static final String EXTRAS_DRIVERID = "driverid";

    @BindView(R.id.complaint_input)
     LinearLayout ll_frist;
    @BindView(R.id.complaint_input_list)
     RecyclerView rcy_coms;
    @BindView(R.id.complaint_input_reason)
     EditText et_reason;

    @BindView(R.id.complaint_history)
     LinearLayout ll_second;
    @BindView(R.id.complaint_history_list)
     RecyclerView rcy_history;
    @BindView(R.id.complaint_history_input)
     EditText et_content;

    @BindView(R.id.title)
    TextView tv_title;

    private String orderSerno;
    private String driverID;

    private ComplaintList complaintList;
    private List<ComplaintList.ComplaintListEntity.ReplyListEntity> replyList;
    private List<Complaint.DatalistEntity> sugList;
    private CompaintSugAdapter sugAdapter;
    private CompaintReplayAdapter replayAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.aty_complaint;
    }

    @Override
    public void init() {
        orderSerno = getIntent().getStringExtra(EXTRAS_ORDERNO);
        driverID = getIntent().getStringExtra(EXTRAS_DRIVERID);

        replyList = new ArrayList<>();

       tv_title.setText(getString(R.string.tousu));

        getComplaintDetails();
    }


    /**
     * 第一次投诉展示内容
     */
    private void showFristComp() {
        ll_frist.setVisibility(View.VISIBLE);
        ll_second.setVisibility(View.GONE);

        InitView.initVritalReclyView(this, rcy_coms);
        sugList = new ArrayList<>();
        sugAdapter = new CompaintSugAdapter(this);
        sugAdapter.setList(sugList);
        rcy_coms.setAdapter(sugAdapter);

        getComplaintSugList();
    }

    /**
     * 已经投诉过得内容展示
     */
    private void showHaveComed() {
        ll_frist.setVisibility(View.GONE);
        ll_second.setVisibility(View.VISIBLE);

        InitView.initVritalReclyView(this, rcy_history);

        replayAdapter = new CompaintReplayAdapter(this);
        replayAdapter.setList(replyList);
        rcy_history.setAdapter(replayAdapter);
    }

    /**
     * 发送回复消息
     */
    private void sendReplay() {
        String replyContent = et_content.getText().toString().trim();
        if (StringUtils.isEmpty(replyContent)) {
            displayShort(getString(R.string.qingshurutousuneirong));
            return;
        }

        et_content.setText("");

        ComplaintList.ComplaintListEntity.ReplyListEntity replyListEntity = fzReplayEntity(ModuleUtils.formatNowDate(null), replyContent, "1");
        if (replayAdapter != null)
            replayAdapter.add(replyListEntity);

        if (complaintList != null)
            ApiWorks.replayCompaint(complaintList.getComplaintList().getComplaintID(), replyContent, null);
    }

    /**
     * 添加投诉
     */
    private void addComplaint() {
        final String content = et_reason.getText().toString().trim();
        Complaint.DatalistEntity info = null;
        if (sugAdapter.selectItem > -1)
            info = sugList.get(sugAdapter.selectItem);

        if (StringUtils.isEmpty(content) && info == null) {
            displayShort(getString(R.string.qingxuanzehuozheshuruneirong));
            return;
        }


        showWaittingDialog("正在提交", false);
        ApiWorks.addCompaint(orderSerno, AppManger.getInstance().user.getWorkNo(), driverID,
                StringUtils.isEmpty(content) ? "" : content, info == null ? "" : info.getCode(), new ApiWorks.ResponseListener<ApiResultCommonStr>() {
                    @Override
                    public void onResponse(ApiResultCommonStr apiResultCommonStr) {
                        if (apiResultCommonStr != null && apiResultCommonStr.getCode() == 1) {
                            getComplaintDetails();
                        }
                    }
                });
    }

    @OnClick(R.id.complaint_history_send)
    public void clickSend(){
        sendReplay();
    }
    @OnClick(R.id.complaint_input_commit)
    public void clickCommit(){
        addComplaint();
    }
    @OnClick(R.id.title_left)
    public void clickBack(){
        finish();
    }

    /**
     * 获取服务器提供的主投诉类型
     */
    private void getComplaintSugList() {
        ApiWorks.getCompainList(new ApiWorks.ResponseListener<Complaint>() {
            @Override
            public void onResponse(Complaint complaint) {
                if (complaint!=null&&complaint.getDatalist()!=null)
                    sugAdapter.appendList(complaint.getDatalist());
            }
        });
    }

    private void getComplaintDetails() {
        ApiWorks.getCompaintByOrderID(orderSerno, new ApiWorks.ResponseListener<ComplaintList>() {
            @Override
            public void onResponse(ComplaintList complaintList) {
                if (complaintList != null) {
                    String complaint = complaintList.getComplaintList().getComplaintContent();
                    String maincom = complaintList.getComplaintList().getMainComplaint();

                    String content = maincom + (StringUtils.isEmpty(complaint) ? "" : ("\n" + complaint));
                    String date = complaintList.getComplaintList().getComplaintTime();

                    ComplaintList.ComplaintListEntity.ReplyListEntity entity = fzReplayEntity(date, content, "1");
                    entity.setMainReplay(!StringUtils.isEmpty(maincom));
                    replyList.add(entity);

                    if (complaintList.getComplaintList().getReplyList() != null && complaintList.getComplaintList().getReplyList().size() > 0)
                        replyList.addAll(complaintList.getComplaintList().getReplyList());

                    showHaveComed();
                } else {
                    showFristComp();
                }
            }
        } );
    }

    private ComplaintList.ComplaintListEntity.ReplyListEntity fzReplayEntity(String date, String content, String type) {
        ComplaintList.ComplaintListEntity.ReplyListEntity entity = new ComplaintList.ComplaintListEntity.ReplyListEntity();
        entity.setReplyContent(content);
        entity.setReplyFrom(type);
        entity.setReplyTime(date);

        return entity;
    }

}
